package com.ap.core.jbehave;

import com.ap.core.util.TypedKey;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * This class is intended to provide a generic mechanism for storing story context between step executions. It is
* intended to be typically cleared before each story so they remain isolated.
 */
public class StoryContext {

    private Map<TypedKey, Object> contextMap = new HashMap<>();

    /**
     * Put the value into the context in a type safe manner.
     * @param key The key that is used to store and retrieve the value that must be of the appropriate type.
     * @param value The value to store against the key of the right type
     * @param <T> The type that is to be stored for later retrieval.
     */
    public <T> void put(TypedKey<T> key, T value){
         contextMap.put(key, value);
    }

    /**
     * Get the value that has been stored for the given TypedKey.
     * @param key The key for which to retrieve the value.
     * @param <T> The type of the value to be retrieved.
     * @return The value, which may be null if nothing has been stored.
     */
    @SuppressWarnings("unchecked")
    public <T> T get(TypedKey<T> key){
        return (T) contextMap.get(key);
    }

    /**
     * Clear out the StoryContext.
     */
    public void clear() {
        contextMap.clear();
    }
    
    public Set<TypedKey> keySet() {
        return contextMap.keySet();
    }

}
